package com.sgb.kjwl.view.ui.fragment.enterprise;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.util.Pair;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.sgb.kjwl.AppLazyFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentDealedCasesBinding;
import com.sgb.kjwl.di.enterprise.DaggerEnterpriseFragComponent;
import com.sgb.kjwl.di.enterprise.EnterpriseFragModule;
import com.sgb.kjwl.model.entity.enterprise.DealedCasesEntity;
import com.sgb.kjwl.utils.ActivityJump;
import com.sgb.kjwl.view.ui.activity.enterprise.DealedCasesListActivity;
import com.sgb.kjwl.view.ui.activity.enterprise.ProfessionalQuoteDetailActivity;
import com.sgb.kjwl.view.ui.adapter.enterprise.DealedCasesAdapter;
import com.sgb.kjwl.viewmodel.enterprise.DealedCasesFragModel;
import com.swgk.core.base.di.AppComponent;

import java.util.List;

import javax.inject.Inject;

public class ServiceDetailDealedFragment extends AppLazyFragment {

    @Inject
    DealedCasesFragModel model;


    private FragmentDealedCasesBinding binding;
    long serviceId = 0;
    DealedCasesAdapter adapter;

    public static ServiceDetailDealedFragment newInstance(long id) {
        ServiceDetailDealedFragment fragment = new ServiceDetailDealedFragment();
        Bundle bundle = new Bundle();
        bundle.putLong("serviceId", id);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        DaggerEnterpriseFragComponent.builder()
                .appComponent(appComponent)
                .enterpriseFragModule(new EnterpriseFragModule(this))
                .build()
                .inject(this);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_dealed_cases, container, false);
        ((ProfessionalQuoteDetailActivity) getActivity()).setChildObjectForPosition(binding.getRoot(), 1);
        if (adapter == null)
            adapter = new DealedCasesAdapter(getContext(), null);

        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        binding.recyclerView.setLayoutManager(layoutManager);
        binding.recyclerView.setAdapter(adapter);
        binding.recyclerView.setLoadingMoreEnabled(false);
        binding.recyclerView.setPullRefreshEnabled(false);

        binding.tvShowMore.setVisibility(View.GONE);
        binding.tvShowMore.setOnClickListener(view -> ActivityJump.jumpToActivity(getContext(), DealedCasesListActivity.class, Pair.create("serviceId", serviceId)));
        return binding.getRoot();
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    protected void onLazyLoad() {
        serviceId = getArguments().getLong("serviceId", 0);
        model.getCasesQueryList(false, serviceId);
    }


    /**
     * 杰出人才数据
     *
     * @param list
     * @param isLoadMore
     */
    public void showList(List<DealedCasesEntity> list, boolean isLoadMore) {
        binding.recyclerView.refreshComplete();
        if (isLoadMore) {
            if (null != list && list.size() > 0) {
                binding.recyclerView.loadMoreComplete();
                adapter.addDatas(list);
            } else {
                binding.recyclerView.setNoMore(true);
            }
            binding.recyclerView.setVisibility(View.VISIBLE);
        } else {
            binding.recyclerView.refreshComplete();
            if (null != list && list.size() > 0) {
                binding.recyclerView.setVisibility(View.VISIBLE);
                binding.emptyView.setVisibility(View.GONE);
                adapter.setDatas(list.size() > 3 ? list.subList(0, 3) : list);
                binding.recyclerView.setLoadingMoreEnabled(false);

                binding.tvShowMore.setVisibility(list.size() > 3 ? View.VISIBLE : View.GONE);

                binding.tvShowMore.getVisibility();
            } else {
                adapter.setDatas(null);
                binding.recyclerView.setVisibility(View.GONE);
                binding.emptyView.setVisibility(View.VISIBLE);
            }
        }
    }
}
